﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Deeper.Common
{
    /// <summary>
    /// 正则表达式扩展方法
    /// </summary>    
    public static class RegexHelper
    {
        /// <summary>
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="pattern">模式字符串</param>
        /// <param name="options">筛选条件,比如是否忽略大小写</param>
        public static bool IsMatch(this string input, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
        {
            return Regex.IsMatch(input, pattern, options);
        }

        /// <summary>
        /// 获取匹配的值
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="pattern">模式字符串</param>
        /// <param name="resultPattern">结果模式字符串,范例："$1"用来获取第一个( )内的值</param>
        /// <param name="options">筛选条件,比如是否忽略大小写</param>
        public static string GetMatchValue(this string input, string pattern, string resultPattern, RegexOptions options = RegexOptions.IgnoreCase)
        {
            //判断是否匹配
            if (Regex.IsMatch(input, pattern, options))
                return Regex.Match(input, pattern, options).Result(resultPattern);
            else
                return string.Empty;
        }


        /// <summary>
        /// 获取匹配的值
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="pattern">模式字符串</param>
        public static string GetMatchValue(this string input, string pattern)
        {
            if (Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase))
            {
                return Regex.Match(input, pattern, RegexOptions.IgnoreCase).Value;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 标签的正则表达.如标签: <tp:for from="1" to="100" index="i">
        /// </summary>
        public static readonly Regex TagRegex;

        /// <summary>
        /// 结束标签的正则表达式.如标签: </tp:for>
        /// </summary>
        public static readonly Regex EndTagRegex;

        /// <summary>
        /// 变量标签的正则表达式.如: {$:member.name} 或带前缀与属性的变量标签: {$:#.member.name htmlencode='true'}
        /// 前缀必须与#号开始.并且与.号结束.
        /// 如变量没有前缀.则表明此变量是根级模板的变量.
        /// 如果变量只有前缀"#."(如#.member.name)则表示当前模板的变量
        /// 如果变量只有前缀"##."(如##.member.name)则表示是当前模板的父级模板的变量.如果不存在父级模板.则归为当前模板.
        /// 如果变量是前缀"#"加其它字符.(如(#t1.member.name).则表示此变量是属于Id为"t1"的模板.如果不存在Id为"t1"的模板.则将产生解析错误.
        /// </summary>
        public static readonly Regex VarTagRegex;

        /// <summary>
        /// 
        /// </summary>
        static RegexHelper()
        {
            string varExp = @"(?:#(?<prefix>#|[\-\w]*)\.)?(?<name>(?!\d)\w+)(?:\.(?<field>\d+|(?!\d)\w+)(?<method>(?:\( *\))?))*";

            TagRegex = new Regex(@"\G<tp\:\s*(?<tagname>[\-\w]+)(\s+(?<attrname>[\-\w]+)(\s*=\s*""(?<attrval>[^""]*)""|\s*=\s*'(?<attrval>[^']*)'|\s*=\s*(?<attrval>[^\s=>]*)|(?<attrval>\s*?)))*\s*(?<closed>/)?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            EndTagRegex = new Regex(@"\G</tp\:\s*(?<tagname>[\-\w]+)(\s+(?<attrname>[\-\w]+)(\s*=\s*""(?<attrval>[^""]*)""|\s*=\s*'(?<attrval>[^']*)'|\s*=\s*(?<attrval>[^\s=>]*)|(?<attrval>\s*?)))*\s*>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            VarTagRegex = new Regex(@"\G{\$\:\s*" + varExp + @"(\s+(?<attrname>[\-\w]+)(\s*=\s*""(?<attrval>[^""]*)""|\s*=\s*'(?<attrval>[^']*)'|\s*=\s*(?<attrval>[^\s=}]*)|(?<attrval>\s*?)))*\s*}", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        }
    }
}
